/* ============================================================================
 Name        : Siko.c
 Author      : LETANOUX Florian 101570
 Version     : 0.0
 Description : B1 project
 ============================================================================ */

#include <stdio.h>
#include <stdlib.h>
#include "card.h"
#include "checkCombinaison.h"

int showCombinaison(card hand[5])
{
    int bestCombinaison = 0;
    int pair = 0, straight = 0, flush = 0, doublePair = 0, full = 0;

    flush = flushCheck(hand);
        if((pair = pairCheck(hand)) == 0)
        {   
        straight = straightCheck(hand);

            if(flush == 1)
                bestCombinaison = 5;
            else if(straight == 1)
                bestCombinaison = 4;

            if (straight == 1 && flush == 1)
                bestCombinaison = 8;

            if(bestCombinaison == 8)
            {
                if(royalStraightFlushCheck(hand) == 1)
                    bestCombinaison = 9;
            }
        }
        else
        {
            bestCombinaison = pair;
            if(bestCombinaison == 2) // On transforme le brelan avec la bonne valeur de sortie
                bestCombinaison = 3;
            else if (bestCombinaison == 3) // On transforme le carre avec la bonne valeur de sortie
                bestCombinaison = 7;

            if(flush == 1 && bestCombinaison <= 5 )
                bestCombinaison = 5;

            if(bestCombinaison == 3)
            {
            full = fullCheck(hand);
                if(full == 1)
                    bestCombinaison = 6;
                else if (flush == 1)
                    bestCombinaison = 5;
            }
            else if(bestCombinaison == 1)
            {
                if (flush == 1)
                    bestCombinaison = 5;

                else
                {
            doublePair = doublePairCheck(hand);
                if(doublePair == 1)
                    bestCombinaison = 2;
                }
            }
           
        }
    
    return bestCombinaison;
}

int pairCheck(card hand[5])
{
    int pair = 0;
    int pairTemp = 0;
    int i = 0, j=1;
        while(j <= 4)
        {
            while(hand[i].value == hand[j].value)
            {
            pair++;
            j++;
            }

            if(pair > 0)
            {
                if(pairTemp < pair)
                    pairTemp = pair;
                pair = 0;
            }
            else
            {
            i = j;
            j++;
            }
        }
    return pairTemp;
}

int doublePairCheck(card hand[5])
{
    int pair = 0;
    int i = 0, j = 1;

    while(j < 5)
    {
        while(hand[i].value == hand[j].value)
        {
        pair++;
        j++;
        }
        i = j;
        j++;
    }
    if(pair == 2)
        return 1;
    else
        return 0;
}

int fullCheck(card hand[5])
{
    int pair = 0;
    int i = 0, j = 1;

        while(j < 5)
        {
            while(hand[i].value == hand[j].value)
            {
            j++;
            pair++;
            }
        i = j;
        j++;
        }
        if(pair == 3)
            return 1;
        else
            return 0;
}

int straightCheck(card hand[5])
{
    int straight = 0;
    int i = 0, j=1;
    card temphand[5];

        for(i = 0;i<5;i++)
            temphand[i] = hand[i];
    
    i =0;
        while( (temphand[i].value == (temphand[j].value -1)) && j <= 4)
        {
         straight++;
         i = j;
         j++;
        }

        if(straight == 4)
            return 1;
        else if (temphand[0].value == 1) // On fait en sorte de prendre l'as comme la carte la plus forte
            {
            temphand[0].value = 9;
            return straightCheck(temphand);
            }
    return 0;
}

int flushCheck(card hand[5])
{
    int flush = 0;
    int i = 0, j=1;

        while( (hand[i].color == hand[j].color) && j <= 4)
        {
         flush++;
         i = j;
         j++;
        }
    if(flush == 4)
        return 1;
    else
        return 0;
}

int royalStraightFlushCheck(card hand[5])
{
    if(hand[0].value == 1 && hand[4].value == 13)
        return 1;
    else
        return 0;
};